package at.fhj.swd.pse.domain;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import at.fhj.swd.pse.entities.Activity;
import at.fhj.swd.pse.entities.Community;
import at.fhj.swd.pse.entities.User;
import at.fhj.swd.pse.data.ActivityDAO;
import at.fhj.swd.pse.data.CommunityDAOMock;

/**
 * Tests the community service component.
 * 
 * @author Helmut Kopf
 * @author Mario Pflegpeter
 * 
 */
public class UTCommunityService {

	/*
	 * This stub is used to run testCreateCommunity without error
	 */
	private final class ActivityServiceStub implements ActivityService {
		@Override
		public List<Activity> getAllActivities(int userId) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Activity createNewActivity(Activity activity) {
			return null;
		}

		@Override
		public Activity createNewActivity(Activity activity, Community community) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void removeActivity(Activity activity) {
			// TODO Auto-generated method stub
		}

		@Override
		public void setA_dao(ActivityDAO a_dao) {
			// TODO Auto-generated method stub
			
		}
	}

	private CommunityService m_service;
	private CommunityDAOMock m_dao;
	private ActivityServiceStub m_as;

	private Community createFirstCommunity() {
		Community community = new Community();

		community.setName("Community1");
		community.setDescription("Community1 Description");
		community.setTopic("Community1 Topic");
		community.setReleased(false);

		return community;
	}

	private Community createSecondCommunity() {
		Community community = new Community();

		community.setName("Community2");
		community.setDescription("Community2 Description");
		community.setTopic("Community2 Topic");

		return community;
	}

	@Before
	public void setUp() {
		m_dao = new CommunityDAOMock();
		m_as = new ActivityServiceStub();
		m_service = new CommunityServiceImpl(m_dao, m_as);
	}

	@Test
	public void testGetCommunities() {
		Community community = createFirstCommunity();

		m_dao.insert(community);

		List<Community> communities = m_service.getCommunities();
		assertEquals(1, communities.size());

		assertEquals(community.getName(), communities.get(0).getName());
		assertEquals(community.getDescription(), communities.get(0).getDescription());
		assertEquals(community.getTopic(), communities.get(0).getTopic());
	}

	@Test
	public void testGetCommunity() {
		Community community1 = createFirstCommunity();

		m_dao.insert(community1);

		Community community2 = createSecondCommunity();
		Community community2_t = m_dao.insert(community2);

		community2 = m_service.getCommunity(community2_t.getID());

		assertNotNull(community2);
		assertEquals(community2_t.getName(), community2.getName());
		assertEquals(community2_t.getDescription(), community2.getDescription());
		assertEquals(community2_t.getTopic(), community2.getTopic());
	}

	@Test
	public void testCreateCommunity() {
		Community community = createFirstCommunity();
		community.setAdministractor(new User());

		m_service.createNewCommunity(community);

		List<Community> communities = m_service.getCommunities();
		assertEquals(1, communities.size());

		assertEquals(community.getName(), communities.get(0).getName());
		assertEquals(community.getDescription(), communities.get(0).getDescription());
		assertEquals(community.getTopic(), communities.get(0).getTopic());
	}

	@Test
	public void testReleaseCommunity() {
		Community testCommunity = createFirstCommunity();
		Community community = m_dao.insert(testCommunity);

		m_service.releaseCommunity(testCommunity);

		assertTrue(community.getReleased());
	}

	@Test
	public void testAddUserToCommunity() {
		Community testCommunity = createFirstCommunity();

		Community community = m_dao.insert(testCommunity);

		User testUser1 = new User();
		testUser1.setId(1);
		
		assertEquals(0, community.getMembers().size());
		
		m_service.addUserToCommunity(testUser1, community);

		List<User> members = community.getMembers();
		assertEquals(1, members.size());
		assertEquals(testUser1.getId(), members.get(0).getId());
	}
}
